/**
 * 
 */
package design_pattern.memento.whiteboxcheckpoints;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @author linzq
 * 
 */
public class Originator {

  private Vector states;

  private int index;

  public Originator() {
    states = new Vector();
    index = 0;
  }

  /**
   * 工厂方法返回一个新的备忘录对象
   * 
   * @return
   */
  public Memento createMemento() {
    return new Memento(states, index);
  }

  public void restoreMemento(Memento m) {
    this.index = m.getIndex();
    this.states = m.getStates();
  }

  public void setState(String state) {
    this.states.add(state);
    this.index++;
  }

  public void printStates() {
    Enumeration e = states.elements();
    while (e.hasMoreElements()) {
      System.out.println("Current state = " + e.nextElement().toString());
    }
  }

}
